package com.cz.run.bumng;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cz.run.beans.mysql.*;
import com.cz.run.bumng.model.PageModel;
import com.cz.run.bumng.model.PocketModel;
import com.cz.run.bumng.model.PocketTransModel;
import com.cz.run.service.IUserBaseService;
import com.cz.run.service.IUserPocketLogService;
import com.cz.run.service.IUserPocketPointLogService;
import com.cz.run.service.IUserPocketService;
import com.cz.run.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by deior on 2018/6/9.
 */
@Controller
@RequestMapping("/bumng")
public class PocketController {

    @Autowired
    private IUserPocketLogService iUserPocketLogService;

    @Autowired
    private IUserBaseService iUserBaseService;

    @Autowired
    private IUserPocketService iUserPocketService;

    @Autowired
    private IUserPocketPointLogService iUserPocketPointLogService;

    @RequestMapping(value ="/pocketLog.htm",method = RequestMethod.GET)
    public String pocketLog(HttpServletRequest request, @ModelAttribute BumngUser bumngUser, Model model){

        BumngUser bumngUser1 = (BumngUser) request.getSession().getAttribute("bumngUser");

        if(null==bumngUser1){
            return "login";
        }
        model.addAttribute("bumngUser",bumngUser1);


        String address = request.getParameter("address");
        model.addAttribute("address",address);

        PageModel pageModel =  PageUtil.getPageData(request);

        EntityWrapper<UserPocketLog> ew = new EntityWrapper<>();
        ew.where("1 = 1").and(StringUtils.hasLength(address),"address = {0}",address).
                orderBy("create_time desc limit "+pageModel.getPageNum()+","+pageModel.getPageSize());
        List<UserPocketLog> userPocketLogs =  iUserPocketLogService.selectList(ew);

        EntityWrapper<UserPocketLog> ew1 = new EntityWrapper<>();
        ew1.where(StringUtils.hasLength(address),"address = {0}",address);
        int count =  iUserPocketLogService.selectCount(ew1);


        model.addAttribute("pageTotal",count%pageModel.getPageSize()>0?count/10+1:count/10);
        model.addAttribute("pageNum",(pageModel.getPageNum())/10+1);

        model.addAttribute("userPocketLogs",userPocketLogs);

        return "pocketLog";
    }


    @RequestMapping(value ="/pocketList.htm",method = RequestMethod.GET)
    public String pocketList(HttpServletRequest request, @ModelAttribute BumngUser bumngUser, Model model){

        BumngUser bumngUser1 = (BumngUser) request.getSession().getAttribute("bumngUser");

        if(null==bumngUser1){
            return "login";
        }
        model.addAttribute("bumngUser",bumngUser1);

        String address = request.getParameter("address");
        String userName = request.getParameter("userName");

        model.addAttribute("address",address);
        model.addAttribute("userName",userName);

        PageModel pageModel =  PageUtil.getPageData(request);

        List<PocketModel> pocketModels = new ArrayList<>();

        BeanCopier bc = BeanCopier.create(UserPocket.class,PocketModel.class,false);

        EntityWrapper<UserPocket> ew = new EntityWrapper<>();
        ew.where("1 = 1").and(StringUtils.hasLength(address),"a.address = {0}",address).and(StringUtils.hasLength(userName),"b.real_name={0}",userName)
        .orderBy("a.create_time desc limit "+pageModel.getPageNum()+","+pageModel.getPageSize());
        List<UserPocket> userPockets =  iUserPocketService.getUserPocketByUserNameAndAddress(ew);

        for(UserPocket userPocket:userPockets) {
            PocketModel pocketModel =new PocketModel();
            bc.copy(userPocket,pocketModel,null);
            EntityWrapper<UserBase> uew = new EntityWrapper<>();
            uew.where("id={0}",userPocket.getUserId());
            UserBase userBase = iUserBaseService.selectOne(uew);
            pocketModel.setUserName(null==userBase?"":userBase.getRealName());
            pocketModels.add(pocketModel);
        }

        EntityWrapper<UserPocket> ew1 = new EntityWrapper<>();
        ew1.where("1 = 1").and(StringUtils.hasLength(address),"a.address = {0}",address).and(StringUtils.hasLength(userName),"b.real_name={0}",userName)
              ;
        int count  =  iUserPocketService.getCountByUserNameAndAddress(ew1);

        model.addAttribute("pageTotal",count%pageModel.getPageSize()>0?count/10+1:count/10);
        model.addAttribute("pageNum",(pageModel.getPageNum())/10+1);
        model.addAttribute("pocketModels",pocketModels);

        return "pocketList";
    }

    @RequestMapping(value ="/pocketTransList.htm",method = RequestMethod.GET)
    public String pocketTransList(HttpServletRequest request, @ModelAttribute BumngUser bumngUser, Model model){

        BumngUser bumngUser1 = (BumngUser) request.getSession().getAttribute("bumngUser");

        if(null==bumngUser1){
            return "login";
        }
        model.addAttribute("bumngUser",bumngUser1);

        String phone = request.getParameter("phone");
        model.addAttribute("phone",phone);

        PageModel pageModel =  PageUtil.getPageData(request);

        List<PocketTransModel> pocketTransModels = new ArrayList<>();

        BeanCopier bc = BeanCopier.create(UserPocketPointLog.class,PocketTransModel.class,false);

        EntityWrapper<UserPocketPointLog> ew = new EntityWrapper<>();
        ew.where("1 = 1").and(StringUtils.hasLength(phone),"b.phone={0}",phone)
                  .orderBy("b.create_time desc limit "+pageModel.getPageNum()+","+pageModel.getPageSize());
        List<UserPocketPointLog> userPocketPointLogs =  iUserPocketPointLogService.getUserPocketPointLogByUserName(ew);

        for(UserPocketPointLog userPocketPointLog:userPocketPointLogs) {
            PocketTransModel pocketTransModel =new PocketTransModel();
            bc.copy(userPocketPointLog,pocketTransModel,null);
            EntityWrapper<UserBase> uew = new EntityWrapper<>();
            uew.where("id={0}",userPocketPointLog.getUserId());
            UserBase userBase = iUserBaseService.selectOne(uew);
            pocketTransModel.setUserName(null==userBase?"":userBase.getRealName());
            pocketTransModel.setPhone(null==userBase?"":userBase.getPhone());
            pocketTransModels.add(pocketTransModel);
        }


        EntityWrapper<UserPocketPointLog> ew1 = new EntityWrapper<>();
        ew1.where("1 = 1").and(StringUtils.hasLength(phone),"b.phone={0}",phone)
               ;
        int count =  iUserPocketPointLogService.getCountByUserName(ew1);

        model.addAttribute("pageTotal",count%pageModel.getPageSize()>0?count/10+1:count/10);
        model.addAttribute("pageNum",(pageModel.getPageNum())/10+1);

        model.addAttribute("pocketTransModels",pocketTransModels);

        return "pocketTransList";
    }

}
